package com.banciyuan.bcywebview.biz.register;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.base.view.dialog.MyProgressDialog;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.apprecommend.RecommendAcgActivity;
import com.banciyuan.bcywebview.biz.pc.PersonHelper;
import com.banciyuan.bcywebview.biz.write.photoselecotor.pselector.PhotoActivity;
import com.banciyuan.bcywebview.utils.file.ImageManager;
import com.banciyuan.bcywebview.utils.http.NetUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.soundcloud.android.crop.Crop;

import java.io.File;

public class RegisterChooseHeadActivity extends BaseActivity implements OnClickListener {

    public final static int REQ_PIC = 1;

    private TextView mSkipTv;
    private TextView mFinishedTv;
    private TextView mNoFinishTv;
    private ImageView mSelectImg;
    private ImageView mHeadImg;

    private PushActionInterface mChangeHeadImageInterface;
    private PersonHelper mHeadHelper;

    private Bitmap bm;

    private MyProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_headimage_layout);

        initArgs();
        initActionbar();
        initUi();
        initAction();
    }

    @Override
    protected void initArgs() {
        mChangeHeadImageInterface = new PushActionInterface() {
            @Override
            public void onNetError(String result) {
                if (mProgressDialog != null && mProgressDialog.isShowing())
                    mProgressDialog.dismiss();
                MyToast.show(RegisterChooseHeadActivity.this, getString(R.string.failtouploadavatar));
            }

            @Override
            public void PushSuccess(String result) {
                UserDataHelper.getInstance(RegisterChooseHeadActivity.this).setAvatar(result);
                if (mProgressDialog != null && mProgressDialog.isShowing())
                    mProgressDialog.dismiss();
                gotoNextActivity();
            }
        };
        mHeadHelper = new PersonHelper(this, mChangeHeadImageInterface);
    }

    private boolean isShowRecommend() {
        return SPHelper.getBoolean(this, SPConstant.BCY_SHOWRECOMMEND, false);
    }

    private void gotoNextActivity() {
        if (isShowRecommend()) {
            gotoUtil.replaceMainClearTop(this);
        } else {
            gotoUtil.replaceAct(this, RecommendAcgActivity.class);
        }
    }

    @Override
    protected void initActionbar() {
    }

    @Override
    protected void initUi() {
        mSkipTv = (TextView) findViewById(R.id.register_headimage_skip_tv);
        mFinishedTv = (TextView) findViewById(R.id.register_headimage_finished_tv);
        mNoFinishTv = (TextView) findViewById(R.id.register_headimage_nofinish_tv);
        mSelectImg = (ImageView) findViewById(R.id.register_headimage_select_img);
        mHeadImg = (ImageView) findViewById(R.id.register_headimage_head_img);

        mNoFinishTv.setVisibility(View.VISIBLE);
        mFinishedTv.setVisibility(View.GONE);

        DialogInterface.OnClickListener cancelOnClick = null;
        DialogInterface.OnCancelListener onCancelClick = null;
        cancelOnClick = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mHeadHelper.cancelAll();
                mProgressDialog.dismiss();
            }
        };
        onCancelClick = new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mHeadHelper.cancelAll();
            }
        };

        MyProgressDialog.Builder mBuilder = new MyProgressDialog.Builder(this);
        mProgressDialog = mBuilder.setCancelButton(cancelOnClick).setOnCancelButton(onCancelClick).setCancelTxt("取消上传").create();
    }

    @Override
    protected void initAction() {
        mSkipTv.setOnClickListener(this);
        mFinishedTv.setOnClickListener(this);
        mSelectImg.setOnClickListener(this);
        mHeadImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_headimage_skip_tv:
                gotoNextActivity();
                break;

            case R.id.register_headimage_finished_tv:
                doSaveHeadImage();
                break;

            case R.id.register_headimage_select_img:
            case R.id.register_headimage_head_img:
                doSelectHeadImage();
                break;

            default:
                break;
        }
    }

    public void doSaveHeadImage() {
        mProgressDialog.show();
        ImageManager.saveTempImage(RegisterChooseHeadActivity.this, bm, PersonHelper.FAT, true, PersonHelper.FAT_SIZE, 100);
        ImageManager.saveTempImage(RegisterChooseHeadActivity.this, bm, PersonHelper.BIG, true, PersonHelper.BIG_SIZE, 90);
        ImageManager.saveTempImage(RegisterChooseHeadActivity.this, bm, PersonHelper.MIDDLE, true, PersonHelper.MIDDLE_SIZE, 90);
        ImageManager.saveTempImage(RegisterChooseHeadActivity.this, bm, PersonHelper.SMALL, true, PersonHelper.SMALL_SIZE, 90);
        mHeadHelper.doPush();
    }

    public void doSelectHeadImage() {
        Intent intent = new Intent(this, PhotoActivity.class);
        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, true);
        startActivityForResult(intent, REQ_PIC);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQ_PIC) {
            try {
                String filepath = data.getExtras().getString("path");
                File tempfile = new File(ImageManager.getTemp(RegisterChooseHeadActivity.this), "bcy_header_temp.jpg");
                File orginFile = new File(filepath);
                Uri tempUri = Uri.fromFile(tempfile);
                Uri orginUri = Uri.fromFile(orginFile);
                Crop.of(orginUri, tempUri).asSquare().start(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, data);
        }
    }

    private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            handlePicResult(Crop.getOutput(result));
            mNoFinishTv.setVisibility(View.GONE);
            mFinishedTv.setVisibility(View.VISIBLE);
        } else if (resultCode == Crop.RESULT_ERROR) {
            MyToast.show(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT);
        }
    }

    private void handlePicResult(Uri uri) {
        if (uri == null) {
            return;
        }

        if (!NetUtils.checkNetwork(this)) {
            MyToast.show(this, getString(R.string.net_check_first));
            return;
        }
        bm = BitmapFactory.decodeFile(uri.getPath());
        mHeadImg.setImageBitmap(bm);
    }
}